package eva.savant.business.ventas.view

import eva.savant.business.ventas.service.TSVenta
import eva.savant.businessobject.vt.Libroventas

import javax.faces.bean.ManagedBean
import javax.faces.bean.SessionScoped
import javax.annotation.PostConstruct

@SessionScoped
@ManagedBean(name = 'ITFacturaAnular')
public class ITFacturaAnular extends Bean {

  String  factura

  Integer ventaId

  Date dateIni, dateEnd

  @PostConstruct
  public void cleanEntity() {

    factura = ''

    ventaId = 0

    entity  = new Libroventas()

    dateIni = dateEnd= new Date()
  }

  public void editEntity(Object id) {

    entity= get(Libroventas.class,id)
  }

  public List findLsEntity() {

    if(ventaId > 0) return find(Libroventas.class,'estado=? and venta.id=?', 'V', ventaId)

    if(factura.length() > 0) return find(Libroventas.class,'estado=? and factura=?', 'V', factura)

    return find(Libroventas.class,'estado=? and fecha between ? and ? order by id', 'V', dateIni,toMidnight(dateEnd))
  }

  public boolean mergeEntity(Object entity) {

    def tsVenta= (TSVenta) getSpringBean('tsVenta')

    entity= tsVenta.anularFacturaLibro(((Libroventas)entity).venta)

    addInfoMessage 'Se anulo correctamente la FACTURA ' + entity?.factura; return entity != null
  }

}